package com.soxing.braindata.service.stat;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.soxing.braindata.dao.domain.request.ResearchSearchRequest;
import com.soxing.braindata.dao.domain.vo.TinyResearch;
import com.soxing.braindata.dao.entity.StatItemResult;
import com.soxing.braindata.dao.entity.Testee;
import com.soxing.braindata.dao.entity.TesteeExperiment;
import com.soxing.braindata.dao.service.IStatItemResultService;
import com.soxing.braindata.dao.service.ITesteeService;
import com.soxing.braindata.domain.enumeration.SexRateEnum;
import com.soxing.braindata.service.manager.IResearchManager;
import com.soxing.common.utils.response.MyPage;
import com.soxing.common.utils.response.WebResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author :durango
 * @date :2022/8/21 15:45
 * @description 性别比例统计实现
 */
@Slf4j
@Service("sexRateStatService")
public class SexRateStatServiceImpl implements IStatService {

    @Resource
    private IResearchManager researchManager;
    @Resource
    private ITesteeService testeeService;
    @Resource
    private IStatItemResultService statItemResultService;

    @Override
    public void stat(Date beginTime, Date endTime, Integer period) {
        ResearchSearchRequest request = new ResearchSearchRequest();
        List<Integer> stateList = Lists.newArrayList();
        stateList.add(1);
        stateList.add(3);
        request.setStateList(stateList);
        request.setPageSize(10000);
        WebResponse<MyPage<TinyResearch>> response = researchManager.search(request);
        List<TinyResearch> records = response.getData().getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return;
        }
        Set<String> subjectPkIdSet = records.stream().map(TinyResearch::getSubjectPkId).collect(Collectors.toSet());
        List<Testee> testeeList = testeeService.list(new LambdaQueryWrapper<Testee>().in(Testee::getSubjectPkId, subjectPkIdSet));
        if (CollectionUtils.isEmpty(testeeList)) {
            return;
        }
        Map<String, List<Testee>> subjectPkId2TesteeListMap = new HashMap<>();
        for (Testee testee : testeeList) {
            String subjectPkId = testee.getSubjectPkId();
            List<Testee> testeeList1 = subjectPkId2TesteeListMap.get(subjectPkId);
            if (testeeList1 == null) {
                testeeList1 = new ArrayList<>();
                subjectPkId2TesteeListMap.put(subjectPkId, testeeList1);
            }
            testeeList1.add(testee);
        }

        subjectPkId2TesteeListMap.forEach((rcode, tList) -> {

            Map<String,List<Testee>> sitePkIdTesteeListMap = tList.stream().collect(Collectors.groupingBy(Testee::getSitePkId));

            sitePkIdTesteeListMap.forEach(
                    (sitePkId, SList) -> {

                        Long womanCount = SList.stream().filter(a -> a.getSex() == 0).count();
                        Long manCount = SList.stream().filter(a -> a.getSex() == 1).count();

                        Date now = new Date();
                        //String sitePkId = SList.get(0).getSitePkId();
                        StatItemResult statItemResult1 = new StatItemResult();
                        statItemResult1.setItemCode(SexRateEnum.WOMAN.getKey());
                        statItemResult1.setItemName(SexRateEnum.WOMAN.getName());
                        statItemResult1.setSubjectPkId(rcode);
                        statItemResult1.setSitePkId(sitePkId);
                        statItemResult1.setStatResult(BigDecimal.valueOf(womanCount));
                        statItemResult1.setCreateTime(now);

                        StatItemResult statItemResult2 = new StatItemResult();
                        statItemResult2.setItemCode(SexRateEnum.MAN.getKey());
                        statItemResult2.setItemName(SexRateEnum.MAN.getName());
                        statItemResult2.setSubjectPkId(rcode);
                        statItemResult2.setSitePkId(sitePkId);
                        statItemResult2.setStatResult(BigDecimal.valueOf(manCount));
                        statItemResult2.setCreateTime(now);

                        statItemResultService.remove(new LambdaQueryWrapper<StatItemResult>()
                                .eq(StatItemResult::getSubjectPkId, rcode)
                                .eq(StatItemResult::getSitePkId, sitePkId)
                                .in(StatItemResult::getItemCode, SexRateEnum.WOMAN.getKey(), SexRateEnum.MAN.getKey()));

                        statItemResultService.saveBatch(Lists.newArrayList(statItemResult1, statItemResult2));


                    }
            );


        });

    }
}
